package com.example.controller;

import com.example.bean.UserBean;
import com.example.dao.FriendDaoImpl;
import com.example.dao.UserDaoImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class FriendController {
    @Autowired
    private FriendDaoImpl friendDaoImpl;
    @Autowired
    private UserDaoImpl userDaoImpl;

    @RequestMapping(value = "api/friends",method = RequestMethod.GET)
    @ResponseBody
    public List<UserBean> findAll(@RequestParam("username") String username){
        List<UserBean> list = friendDaoImpl.findAllFriendsByUsername(username);
        for (UserBean user : list) {
            System.out.println(user.getUsername()+"   ID: "+user.getId()+"   Experience: "+user.getExp()+"  Level:  "+user.getLevel());
        }
        return list;
    }


    @RequestMapping(value = "api/addFriends",method = RequestMethod.POST)
    @ResponseBody
    public boolean addFriend(@RequestParam("username") String username , @RequestParam("friend") String friend){
        List<UserBean> users = userDaoImpl.findByUserName(friend);
        if(users.isEmpty()) return  false;
        return friendDaoImpl.addFriend(username,friend);
    }

    @RequestMapping(value = "api/deleteFriends",method = RequestMethod.POST)
    @ResponseBody
    public boolean deleteFriend(@RequestParam("username") String username , @RequestParam("friend") String friend){
        List<UserBean> users = userDaoImpl.findByUserName(friend);
        if(users.isEmpty()) return  false;
        return friendDaoImpl.deleteFriend(username,friend);
    }
}
